
Imports System.IO
Imports std
Imports storoomc

Public Module storclientMain

    <ThreadStatic()> Private talk As tcpTalk = Nothing

    Private Sub printHelp()
        Console.WriteLine("valid commands -write, -uniqueWrite, -delete, -modify, -read, -seek, -capacity, " + _
                                        "-valuesize, -list, -status, -health, -full, -retire, -sizeof, -keycount, " + _
                                        "-amutest, -amutest2, -sync, " + _
                                        "-serialRead, -serialWrite, -serialModify, " + _
                                        "-serialSizeof, -serialList, -serialSeek.")
        Console.WriteLine("server selection, -host [host_ip or host_name], -port [port].")
        Console.WriteLine("io selection, -o [output_file_name], -i [input_file_name].")
        Console.WriteLine("key selection, -key [key].")
        Console.WriteLine("talk holdtime selection, -holdtime [time_in_ms].")
        Console.WriteLine("talk livetime selection, -livetime [time_in_ms].")
        Console.WriteLine("for amutest, -testsize [testsize].")
        Console.WriteLine("for sync, -desthost [destination_ip or destination_name], -destport [destination_port].")
    End Sub

#If RETIRED Then

    Private Sub list(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                    , ByVal reader As TextReader, ByVal writer As TextWriter _
                    , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim l As [set](Of String) = Nothing
        Dim rtn As returnValue
        rtn = storoomc.storclient.list(host, port, l, holdtime, livetime)
        If rtn = returnValue.success Then
            debugRaiseError("start output list to writer")
            Dim i As [set](Of String).iterator = Nothing
            i = l.begin()
            While i <> l.end()
                writer.WriteLine(+i)
                i += 1
            End While
            raiseError("success")
        Else
            raiseError("cannot get list from server, host " + host + ", port " + Convert.ToString(port) _
                        + ", error description " + returnValueDescription(rtn), errorHandle.errorType.exclamation)
        End If
    End Sub

#End If

    Private Sub list(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32, _
                     ByVal reader As TextReader, ByVal writer As TextWriter, _
                     ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim l As vector(Of String) = Nothing
        Dim rtn As returnValue
        rtn = storoomc.storclient.list(talk, host, port, l, holdtime, livetime)
        If rtn = returnValue.success Then
            debugRaiseError("start output list to writer")
            Dim i As Int64
            For i = 0 To l.size() - 1
                writer.WriteLine(l(i))
            Next
            raiseError("success")
        Else
            raiseError("cannot get list from server, host " + host + ", port " + Convert.ToString(port) _
                        + ", error description " + returnValueDescription(rtn), errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub status(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                      , ByVal reader As TextReader, ByVal writer As TextWriter _
                      , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim allSlicesCount As UInt64 = 0
        Dim unhealthSlicesCount As UInt64 = 0
        Dim rtn As returnValue
        rtn = storoomc.storclient.status(talk, host, port, allSlicesCount, unhealthSlicesCount, holdtime, livetime)
        If rtn = returnValue.success Then
            writer.WriteLine("status from server, host " + host + ", port " + Convert.ToString(port))
            writer.WriteLine("all_slices_count " + Convert.ToString(allSlicesCount))
            writer.WriteLine("unhealth_slices_count " + Convert.ToString(unhealthSlicesCount))
            writer.WriteLine("health_slices_count " + Convert.ToString(allSlicesCount - unhealthSlicesCount))
        Else
            raiseError("cannot get status from server, host " + host + ", port " + Convert.ToString(port) _
                        + ", error description " + returnValueDescription(rtn), errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub full(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                    , ByVal reader As TextReader, ByVal writer As TextWriter _
                    , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim rtn As returnValue
        rtn = storoomc.storclient.full(talk, host, port, holdtime, livetime)
        Dim full As Boolean
        If rtn = returnValue.success Then
            full = True
        ElseIf rtn = returnValue.fail Then
            full = False
        End If
        If rtn = returnValue.success OrElse rtn = returnValue.fail Then
            raiseError("full status from server, host " + host + ", port " + Convert.ToString(port) _
                        + " is " + Convert.ToString(full))
        Else
            raiseError("cannot get full status from server, host " + host + ", port " + Convert.ToString(port) _
                       + ", error description " + returnValueDescription(rtn), errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub health(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                      , ByVal reader As TextReader, ByVal writer As TextWriter _
                      , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim rtn As returnValue
        rtn = storoomc.storclient.health(talk, host, port, holdtime, livetime)
        Dim health As Boolean = True
        If rtn = returnValue.success Then
            health = True
        ElseIf rtn = returnValue.fail Then
            health = False
        End If
        If rtn = returnValue.success OrElse rtn = returnValue.fail Then
            raiseError("health status from server, host " + host + ", port " + Convert.ToString(port) _
                        + " is " + Convert.ToString(health))
        Else
            raiseError("cannot get health status from server, host " + host + ", port " + Convert.ToString(port) _
                       + ", error description " + returnValueDescription(rtn), errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub readImpl(ByVal host As String, ByVal port As Int32, ByVal key As String, _
                         ByVal writer As TextWriter, ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim buff As bytes = Nothing
        Dim rtn As returnValue
        rtn = storoomc.storclient.read(talk, host, port, key, buff, holdtime, livetime)
        If rtn = returnValue.success Then
            writer.Write(encoding.utf8.GetString(+buff))
            raiseError("success")
        Else
            raiseError("cannot read from server, host " + host + ", port " + Convert.ToString(port) _
                        + ", key " + key + ", error description " + returnValueDescription(rtn) _
                        , errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub read(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                     , ByVal reader As TextReader, ByVal writer As TextWriter _
                     , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim key As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            readImpl(host, port, key, writer, holdtime, livetime)
        ElseIf Not reader Is Nothing Then
            key = reader.ReadLine()
            While Not key Is Nothing
                readImpl(host, port, key, writer, holdtime, livetime)
                key = reader.ReadLine()
            End While
        Else
            raiseError("do not have key in argument, read command needs a key.", errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub write(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                      , ByVal reader As TextReader, ByVal writer As TextWriter _
                      , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim key As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            Dim rtn As returnValue
            rtn = storoomc.storclient.write(talk, host, port, key, _
                                            New bytes(encoding.utf8.GetBytes(reader.ReadToEnd())), holdtime, livetime)
            If rtn <> returnValue.success Then
                raiseError("cannot write to stornode, host " + host + ", port " + Convert.ToString(port) _
                            + ", key " + key + ", error description " + returnValueDescription(rtn) _
                            , errorHandle.errorType.exclamation)
            Else
                raiseError("success")
            End If
        Else
            raiseError("do not have key in argument, write command needs a key.", errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub uniqueWrite(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                            , ByVal reader As TextReader, ByVal writer As TextWriter _
                            , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim key As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            Dim rtn As returnValue
            rtn = storoomc.storclient.uniqueWrite(talk, host, port, key, _
                                                  New bytes(encoding.utf8.GetBytes(reader.ReadToEnd())), _
                                                  holdtime, livetime)
            If rtn <> returnValue.success Then
                raiseError("cannot uniqueWrite to stornode, host " + host + ", port " + Convert.ToString(port) _
                            + ", key " + key + ", error description " + returnValueDescription(rtn) _
                            , errorHandle.errorType.exclamation)
            Else
                raiseError("success")
            End If
        Else
            raiseError("do not have key in argument, write command needs a key.", errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub deleteImpl(ByRef host As String, ByVal port As Int32, ByRef key As String, _
                           ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim rtn As returnValue
        rtn = storoomc.storclient.delete(talk, host, port, key, holdtime, livetime)
        If rtn = returnValue.success Then
            raiseError("delete " + key + " success")
        Else
            raiseError("cannot delete from server, host " + host + ", port " + Convert.ToString(port) _
                        + ", key " + key + ", error description " + returnValueDescription(rtn) _
                        , errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub delete(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32, _
                       ByVal reader As TextReader, ByVal writer As TextWriter, _
                       ByVal holdtime As Int64, ByVal livetime As Int64)
        On Error Resume Next

        Dim key As String = Nothing
        Dim filename As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            deleteImpl(host, port, key, holdtime, livetime)
        Else
            Dim wp As Threading.ParameterizedThreadStart = _
                Sub(obj As Object)
                    deleteImpl(host, port, obj, holdtime, livetime)
                End Sub
            Const threadCount As Int32 = 8
            Dim tp As threadpool = Nothing
            tp = New threadpool(wp, threadCount - 1)
            key = reader.ReadLine()
            While Not key Is Nothing
                If rnd(0, threadCount, True) = 0 Then
                    deleteImpl(host, port, key, holdtime, livetime)
                Else
                    tp.insertJob(key)
                End If
                key = reader.ReadLine()
            End While
            While tp.queueLength() > 0
                sleep()
            End While
            sleepSeconds(16)
            tp.stop()
        End If
    End Sub

    Private Sub modify(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                       , ByVal reader As TextReader, ByVal writer As TextWriter _
                       , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim key As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            Dim rtn As returnValue
            rtn = storoomc.storclient.modify(talk, host, port, key, _
                                             New bytes(encoding.utf8.GetBytes(reader.ReadToEnd())), holdtime, livetime)
            If rtn <> returnValue.success Then
                raiseError("cannot modify to stornode, host " + host + ", port " + Convert.ToString(port) _
                            + ", key " + key + ", error description " + returnValueDescription(rtn) _
                            , errorHandle.errorType.exclamation)
            Else
                raiseError("success")
            End If
        Else
            raiseError("do not have key in argument, modify command needs a key.", errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub seekImpl(ByVal host As String, ByVal port As Int32, ByVal key As String, _
                         ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim rtn As returnValue
        rtn = storoomc.storclient.seek(talk, host, port, key, holdtime, livetime)
        If rtn = returnValue.success Then
            raiseError("found the key " + key + " from server, host " + host + ", port " + Convert.ToString(port) _
                        , errorHandle.errorType.information)
        ElseIf rtn = returnValue.fail Then
            raiseError("do not find the key from server, host " + host + ", port " + Convert.ToString(port) _
                        + ", key " + key, errorHandle.errorType.exclamation)
        Else
            raiseError("cannot finish seek operation from server, host " + host + ", port " + Convert.ToString(port) _
                       + ", key " + key + ", error description " + returnValueDescription(rtn) _
                       , errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub seek(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32, _
                     ByVal reader As TextReader, ByVal writer As TextWriter, _
                     ByVal holdtime As Int64, ByVal livetime As Int64)
        On Error Resume Next

        Dim key As String = Nothing
        Dim filename As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            seekImpl(host, port, key, holdtime, livetime)
        Else
            key = reader.ReadLine
            While Not key Is Nothing
                seekImpl(host, port, key, holdtime, livetime)
                key = reader.ReadLine
            End While
        End If
    End Sub

    Private Sub capacity(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                         , ByVal reader As TextReader, ByVal writer As TextWriter _
                         , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim cap As UInt64 = 0
        Dim rtn As returnValue
        rtn = storoomc.storclient.capacity(talk, host, port, cap, holdtime, livetime)
        If rtn = returnValue.success Then
            raiseError("capacity info from server, host " + host + ", port " + Convert.ToString(port) + _
                       " " + memorySizeDisplay(cap))
        Else
            raiseError("cannot get capacity info from server, host " + host + ", port " + Convert.ToString(port) _
                        + ", error description " + returnValueDescription(rtn), errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub valuesize(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                          , ByVal reader As TextReader, ByVal writer As TextWriter _
                          , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim size As UInt64 = 0
        Dim rtn As returnValue
        rtn = storoomc.storclient.valuesize(talk, host, port, size, holdtime, livetime)
        If rtn = returnValue.success Then
            raiseError("valuesize info from server, host " + host + ", port " + Convert.ToString(port) + _
                       " " + memorySizeDisplay(size))
        Else
            raiseError("cannot get valuesize info from server, host " + host + ", port " + Convert.ToString(port) _
                        + ", error description " + returnValueDescription(rtn), errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub retire(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                       , ByVal reader As TextReader, ByVal writer As TextWriter _
                       , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim rtn As returnValue
        rtn = storoomc.storclient.retire(talk, host, port, holdtime, livetime)
        If rtn = returnValue.success Then
            raiseError("success retire, host " + host + ", port " + Convert.ToString(port))
        Else
            raiseError("failed to retire, host " + host + ", port " + Convert.ToString(port) _
                        + ", error description " + returnValueDescription(rtn), errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub sizeof(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                       , ByVal reader As TextReader, ByVal writer As TextWriter _
                       , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim key As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            Dim rtn As returnValue
            Dim size As Int64
            rtn = storoomc.storclient.sizeof(talk, host, port, key, size, holdtime, livetime)
            If rtn = returnValue.success Then
                raiseError("sizeof " + key + " is " + Convert.ToString(size))
            Else
                raiseError("cannot get sizeof " + key + " from server " + displayHostPort(host, port) _
                            + ", error description " + returnValueDescription(rtn), errorHandle.errorType.exclamation)
            End If
        Else
            raiseError("do not have key in argument, sizeof command needs a key.", errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub keycount(ByVal args As map(Of String, vector(Of String)), ByVal host As String, ByVal port As Int32 _
                         , ByVal reader As TextReader, ByVal writer As TextWriter _
                         , ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim rtn As returnValue
        Dim count As Int64
        rtn = storoomc.storclient.keycount(talk, host, port, count, holdtime, livetime)
        If rtn = returnValue.success Then
            raiseError("keycount is " + Convert.ToString(count))
        Else
            raiseError("cannot get keycout from server, host " + host + ", port " + Convert.ToString(port) _
                        + ", error description " + returnValueDescription(rtn), errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub serialWrite(ByVal args As map(Of String, vector(Of String)), _
                            ByVal host As String, ByVal port As Int32, _
                            ByVal reader As TextReader, ByVal writer As TextWriter, _
                            ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim key As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            Dim r As StreamReader = Nothing
            r = convertor.cast(Of StreamReader)(reader)
            If serialstore.write(host, port, key, r.BaseStream, , holdtime, livetime) Then
                raiseError("success")
            Else
                raiseError("cannot write " + key + " to server, host " + host + ", port " + Convert.ToString(port), _
                           errorHandle.errorType.exclamation)
            End If
        Else
            raiseError("do not have key in argument, serialWrite command needs a key.", _
                       errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub srcb(ByVal buff As bytes, ByVal o As Object)
        Dim s As Stream = Nothing
        s = convertor.cast(Of Stream)(o)
        s.Write(buff, 0, buff.size())
    End Sub

    Private Sub serialRead(ByVal args As map(Of String, vector(Of String)), _
                           ByVal host As String, ByVal port As Int32, _
                           ByVal reader As TextReader, ByVal writer As TextWriter, _
                           ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim key As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            Dim w As StreamWriter = Nothing
            w = convertor.cast(Of StreamWriter)(writer)
            If serialstore.read(host, port, key, AddressOf srcb, w.BaseStream, , holdtime, livetime) Then
                raiseError("success")
            Else
                raiseError("cannot read " + key + " from server, host " + host + ", port " + Convert.ToString(port), _
                           errorHandle.errorType.exclamation)
            End If
        Else
            raiseError("do not have key in argument, serialRead command needs a key.", _
                       errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub serialDelete(ByVal args As map(Of String, vector(Of String)), _
                             ByVal host As String, ByVal port As Int32, _
                             ByVal reader As TextReader, ByVal writer As TextWriter, _
                             ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim key As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            If serialstore.delete(host, port, key, , holdtime, livetime) Then
                raiseError("success")
            Else
                raiseError("cannot delete " + key + " from server, host " + host + ", port " + Convert.ToString(port), _
                           errorHandle.errorType.exclamation)
            End If
        Else
            raiseError("do not have key in argument, serialDelete command needs a key.", _
                       errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub serialModify(ByVal args As map(Of String, vector(Of String)), _
                             ByVal host As String, ByVal port As Int32, _
                             ByVal reader As TextReader, ByVal writer As TextWriter, _
                             ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim key As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            Dim r As StreamReader = Nothing
            r = convertor.cast(Of StreamReader)(reader)
            If serialstore.modify(host, port, key, r.BaseStream, , holdtime, livetime) Then
                raiseError("success")
            Else
                raiseError("cannot write " + key + " to server, host " + host + ", port " + Convert.ToString(port), _
                           errorHandle.errorType.exclamation)
            End If
        Else
            raiseError("do not have key in argument, serialModify command needs a key.", _
                       errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub serialList(ByVal args As map(Of String, vector(Of String)), _
                           ByVal host As String, ByVal port As Int32, _
                           ByVal reader As TextReader, ByVal writer As TextWriter, _
                           ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim l As vector(Of String) = Nothing
        If serialstore.list(host, port, l, , holdtime, livetime) Then
            If Not l Is Nothing Then
                Dim i As Int64
                For i = 0 To l.size() - 1
                    writer.WriteLine(l(i))
                Next
            End If
            raiseError("success")
        Else
            raiseError("cannot serial list from server " + displayHostPort(host, port), _
                       errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub serialSizeof(ByVal args As map(Of String, vector(Of String)), _
                             ByVal host As String, ByVal port As Int32, _
                             ByVal reader As TextReader, ByVal writer As TextWriter, _
                             ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim key As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            Dim size As UInt64
            If serialstore.sizeof(host, port, key, size, , holdtime, livetime) Then
                raiseError("sizeof " + key + " is " + Convert.ToString(size))
            Else
                raiseError("cannot get sizeof " + key + " from server " + displayHostPort(host, port), _
                           errorHandle.errorType.exclamation)
            End If
        Else
            raiseError("do not have key in argument, sizeof command needs a key.", errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub serialSeekImpl(ByVal host As String, ByVal port As Int32, ByVal key As String, _
                               ByVal holdtime As Int64, ByVal livetime As Int64)
        If serialstore.seek(host, port, key, storoom, holdtime, livetime) Then
            raiseError("found the key " + key + " from server " + displayHostPort(host, port), _
                       errorHandle.errorType.information)
        Else
            raiseError("do not find the key " + key + " from server " + displayHostPort(host, port), _
                       errorHandle.errorType.warning)
        End If
    End Sub

    Private Sub serialSeek(ByVal args As map(Of String, vector(Of String)), _
                           ByVal host As String, ByVal port As Int32, _
                           ByVal reader As TextReader, ByVal writer As TextWriter, _
                           ByVal holdtime As Int64, ByVal livetime As Int64)
        On Error Resume Next

        Dim key As String = Nothing
        Dim filename As String = Nothing
        If argmgr.getfirstvalue(args, "key", key) Then
            serialSeekImpl(host, port, key, holdtime, livetime)
        Else
            key = reader.ReadLine
            While Not key Is Nothing
                serialSeekImpl(host, port, key, holdtime, livetime)
                key = reader.ReadLine
            End While
        End If
    End Sub

    Private Sub version(ByVal args As map(Of String, vector(Of String)), _
                        ByVal host As String, ByVal port As Int32, _
                        ByVal reader As TextReader, ByVal writer As TextWriter, _
                        ByVal holdtime As Int64, ByVal livetime As Int64)
        On Error Resume Next
        Dim ver As String = Nothing
        If storclient.version(talk, host, port, ver, holdtime, livetime) = returnValue.success Then
            raiseError("version info from server " + displayHostPort(host, port) + " is " + ver)
        Else
            raiseError("cannot get valid response from server " + displayHostPort(host, port), _
                       errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub sync(ByVal args As map(Of String, vector(Of String)), _
                     ByVal host As String, ByVal port As Int32, _
                     ByVal reader As TextReader, ByVal writer As TextWriter, _
                     ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim desthost As String = Nothing
        Dim destportStr As String = Nothing
        Dim destport As Int32 = npos
        If argmgr.getfirstvalue(args, "desthost", desthost) AndAlso _
           argmgr.getfirstvalue(args, "destport", destportStr) AndAlso _
           convertor.toint32(destportStr, destport) Then
            Dim rtn As returnValue = returnValue.fail
            rtn = storoomc.storclient.sync(talk, host, port, desthost, destport, holdtime, livetime)
            If rtn = returnValue.success Then
                raiseError("success")
            Else
                raiseError("cannot sync from server " + displayHostPort(host, port) + _
                           " to " + displayHostPort(desthost, destport) + ", " + _
                            "error description " + returnValueDescription(rtn), errorHandle.errorType.exclamation)
            End If
        Else
            raiseError("cannot get valid destination host / port from arguments.", errorHandle.errorType.user)
        End If
    End Sub

    Private Sub initialStoroom(ByVal host As String, ByVal port As Int32, _
                               ByVal holdtime As Int64, ByVal livetime As Int64)
        storoom.initial(host, port)
        storoom.holdTime = holdtime
        storoom.liveTime = livetime
    End Sub

    Private Sub directorywrite(ByVal args As map(Of String, vector(Of String)), _
                               ByVal host As String, ByVal port As Int32, _
                               ByVal reader As TextReader, ByVal writer As TextWriter, _
                               ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim directory As String = Nothing
        Dim file As String = Nothing
        If argmgr.getfirstvalue(args, "directory", directory) AndAlso _
           argmgr.getfirstvalue(args, "file", file) Then
            Dim buff() As Byte = Nothing
            Try
                buff = IO.File.ReadAllBytes(file)
            Catch ex As Exception
                raiseError(errorHandle.errorType.warning, "cannot read file ", file, ", ex", ex.Message)
            End Try
            file = filename(file)
            initialStoroom(host, port, holdtime, livetime)
            If Not directorystore.writefile(directory, file, New bytes(buff), storoom) Then
                raiseError(errorHandle.errorType.warning, "cannot write file ", file, _
                           " to directory ", directory, ", server ", displayHostPort(host, port))
            End If
        Else
            raiseError(errorHandle.errorType.user, "cannot get valid directory / file from arguments.")
        End If
    End Sub

    Private Sub directoryread(ByVal args As map(Of String, vector(Of String)), _
                              ByVal host As String, ByVal port As Int32, _
                              ByVal reader As TextReader, ByVal writer As TextWriter, _
                              ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim directory As String = Nothing
        Dim file As String = Nothing
        If argmgr.getfirstvalue(args, "directory", directory) AndAlso _
           argmgr.getfirstvalue(args, "file", file) Then
            file = filename(file)
            initialStoroom(host, port, holdtime, livetime)
            Dim buff As bytes = Nothing
            If directorystore.readfile(directory, file, buff, storoom) Then
                If istype(Of StreamWriter, TextWriter)(writer) Then
                    convertor.cast(Of StreamWriter)(writer).BaseStream.Write(+buff, 0, buff.size())
                Else
                    writer.Write(encoding.utf8().GetString(buff))
                End If
            Else
                raiseError(errorHandle.errorType.warning, "cannot read file ", file, _
                           " from directory ", directory, ", server ", displayHostPort(host, port))
            End If
        Else
            raiseError(errorHandle.errorType.user, "cannot get valid directory / file from arguments.")
        End If
    End Sub

    Private Sub directorylist(ByVal args As map(Of String, vector(Of String)), _
                              ByVal host As String, ByVal port As Int32, _
                              ByVal reader As TextReader, ByVal writer As TextWriter, _
                              ByVal holdtime As Int64, ByVal livetime As Int64)
        Dim directory As String = Nothing
        If argmgr.getfirstvalue(args, "directory", directory) Then
            initialStoroom(host, port, holdtime, livetime)
            Dim vs As vector(Of String) = Nothing
            If directorystore.listfiles(directory, vs, storoom) Then
                For i As Int64 = 0 To vs.size() - 1
                    writer.WriteLine(vs(i))
                Next
            Else
                raiseError(errorHandle.errorType.warning, _
                           "cannot list files from directory ", directory, ", server ", displayHostPort(host, port))
            End If
        Else
            raiseError(errorHandle.errorType.user, "cannot get valid directory from arguments.")
        End If
    End Sub

    Public Sub main(ByVal iargs() As String)
        'debugpause(True)

        errorHandle.initial(errorHandle.NOLOGFILEMARK)
        counter.initial(counter.NOCOUNTERFILEMARK)

        Dim args As map(Of String, vector(Of String)) = Nothing
        argmgr.parse(iargs, args)

        If argmgr.contains(args, "h") OrElse argmgr.contains(args, "help") Then
            printHelp()
        Else
            Dim host As String = Nothing
            If Not argmgr.getfirstvalue(args, "host", host) Then
                raiseError("do not get host parameter, use default value as " + network.localhost _
                           , errorHandle.errorType.user)
                host = network.localhost
            End If
            Dim str As String = Nothing
            Dim port As Int32
            If Not (argmgr.getfirstvalue(args, "port", str) AndAlso convertor.toint32(str, port)) Then
                raiseError("do not get port parameter, use default value as " _
                           + Convert.ToString(protocol.port.storoom), errorHandle.errorType.user)
                port = protocol.port.storoom
            End If
            Dim holdtime As Int64
            If Not (argmgr.getfirstvalue(args, "holdtime", str) AndAlso convertor.toint64(str, holdtime)) Then
                raiseError("do not get holdtime parameter, use default value as " _
                           + Convert.ToString(talkTime.client2room.holdtime), errorHandle.errorType.user)
                holdtime = talkTime.client2room.holdtime
            End If
            Dim livetime As Int64
            If Not (argmgr.getfirstvalue(args, "livetime", str) AndAlso convertor.toint64(str, livetime)) Then
                raiseError("do not get livetime parameter, use default value as " _
                            + Convert.ToString(talkTime.client2room.livetime), errorHandle.errorType.user)
                livetime = talkTime.client2room.livetime
            End If
            Dim filename As String = Nothing
            Dim reader As TextReader = Nothing
            If argmgr.getfirstvalue(args, "i", filename) Then
                Try
                    reader = New StreamReader(New FileStream(filename, FileMode.Open _
                                                            , FileAccess.Read, FileShare.ReadWrite))
                Catch ex As Exception
                    raiseError("cannot open file " + filename + ", " + ex.Message, errorHandle.errorType.user)
                End Try
            Else
                reader = Console.In
            End If
            assert(Not reader Is Nothing, "cannot initial reader.")

            Dim writer As TextWriter = Nothing
            If argmgr.getfirstvalue(args, "o", filename) Then
                Try
                    writer = New StreamWriter(New FileStream(filename, FileMode.Create, _
                                                             FileAccess.Write, FileShare.Read))
                Catch ex As Exception
                    raiseError("cannot open file " + filename + ", " + ex.Message, errorHandle.errorType.user)
                End Try
            Else
                writer = Console.Out
            End If
            assert(Not writer Is Nothing, "cannot initial writer.")

            If argmgr.contains(args, "list") Then
                list(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "status") Then
                status(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "read") Then
                read(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "write") Then
                write(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "uniqueWrite") Then
                uniqueWrite(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "delete") Then
                delete(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "modify") Then
                modify(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "read") Then
                read(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "seek") Then
                seek(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "capacity") Then
                capacity(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "valuesize") Then
                valuesize(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "health") Then
                health(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "full") Then
                full(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "retire") Then
                retire(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "sizeof") Then
                sizeof(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "keycount") Then
                keycount(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "amutest") Then
                amutest(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "amutest2") Then
                amutest2(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "serialRead") Then
                serialRead(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "serialWrite") Then
                serialWrite(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "serialDelete") Then
                serialDelete(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "serialModify") Then
                serialModify(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "serialList") Then
                serialList(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "serialSizeof") Then
                serialSizeof(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "ver") Then
                version(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "directoryWrite") Then
                directorywrite(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "directoryRead") Then
                directoryread(args, host, port, reader, writer, holdtime, livetime)
            ElseIf argmgr.contains(args, "directoryList") Then
                directorylist(args, host, port, reader, writer, holdtime, livetime)
            Else
                raiseError("cannot got valid command from arguments, use -h or -help for a short help." _
                            , errorHandle.errorType.exclamation)
            End If

            closereader(reader)
            closewriter(writer)
        End If
    End Sub

End Module
